﻿using simulate_da_click.BusinessLogic;
using simulate_da_click.Configuration;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace simulate_da_click {
    public partial class MainForm : Form {

        #region Fields
        private SimulationConfiguration _simulationConfiguration = new SimulationConfiguration();
        private SimulationRuntime _simulationRuntime;
        private Hotkey _hotkey;
        #endregion

        #region CTor
        public MainForm() {
            InitializeComponent();
            this.FormClosing += MainForm_FormClosing;


            _hotkey = Hotkey.Register(Keys.Escape);
            _hotkey.HotkeyPressed += _hotkey_HotkeyPressed;

            InitializeIntervallTimes();
            Load += MainForm_Load;
        }

        void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
            _hotkey.Dispose();
        }

        void _hotkey_HotkeyPressed(object sender, EventArgs e) {
            if (_simulationRuntime != null)
                _simulationRuntime.StopSimulation();
        }

        #endregion

        #region IntervallTimes
        private void InitializeIntervallTimes() {
            var intervalTimeNames = Enum.GetNames(typeof(IntervalTimeEnum));
            foreach (var intervalTimeName in intervalTimeNames) {
                _cboIntervallTimeSelection.Items.Add(intervalTimeName);
            }

            _cboIntervallTimeSelection.SelectedIndex = 0;
            _cboIntervallTimeSelection.SelectedIndexChanged += _cboIntervallTimeSelection_SelectedIndexChanged;
        }

        void _cboIntervallTimeSelection_SelectedIndexChanged(object sender, EventArgs e) {
            var cbo = sender as ComboBox;
            if (cbo == null) {
                _simulationConfiguration.IntervalTime = IntervalTimeEnum.Second;
            } else {
                try {
                    var selectedValue = cbo.SelectedValue.ToString();
                    _simulationConfiguration.IntervalTime = (IntervalTimeEnum)Enum.Parse(typeof(IntervalTimeEnum), selectedValue);
                } catch {
                    _simulationConfiguration.IntervalTime = IntervalTimeEnum.Second;
                    MessageBox.Show("The selected value is not recognizable.");
                }
            }
        } 
        #endregion

        void MainForm_Load(object sender, EventArgs e) {

        }

        private void _btnRecordLocation_Click(object sender, EventArgs e) {
            var initText = _btnRecordLocation.Text;

            _btnRecordLocation.Text = "REC in 3";
            Pause(1);
            _btnRecordLocation.Text = "REC in 2";
            Pause(1);

            _btnRecordLocation.Text = "REC in 1";
            Pause(1);

            _btnRecordLocation.Text = "Recorded";
            _simulationConfiguration.TargetMouseLocation = Cursor.Position;
            _lblLocation.Text = _simulationConfiguration.TargetMouseLocation.ToString();
            Pause(1);

            _btnRecordLocation.Text = initText;
        }

        private void Pause(int seconds) {
            Application.DoEvents();
            Thread.Sleep(seconds * 1000);
            Application.DoEvents();
        }

        private void _btnRecMovement_Click(object sender, EventArgs e) {
            
        }

        private void _txtClicks_TextChanged(object sender, EventArgs e) {
            var inputSender = (TextBox)sender;
            var value = GetInputAsInt(inputSender.Text);
            if (value == null)
                inputSender.Text = "1";
            else
                _simulationConfiguration.ClicksCount = (int)value;
        }

        private void _txtPer_TextChanged(object sender, EventArgs e) {
            var inputSender = (TextBox)sender;
            var value = GetInputAsInt(inputSender.Text);
            if (value == null)
                inputSender.Text = "1";
            else
                _simulationConfiguration.Interval = (int)value;
        }

        private int? GetInputAsInt(string input) {
            try {
                return int.Parse(input);
            } catch{
                return null;
            }
        }

        private void _btnStartSimulation_Click(object sender, EventArgs e) {
            _simulationRuntime = new SimulationRuntime(_simulationConfiguration);

            var initText = _btnStartSimulation.Text;

            for (int i = _simulationConfiguration.Countdown; i > 0; i--) {
                _btnStartSimulation.Text = i.ToString();
                Pause(1);
            }
            _btnStartSimulation.Text = "Running...";

            _simulationRuntime.StartSimulation();

            _btnStartSimulation.Text = initText;
        }

        private void _tBoxCountdown_TextChanged(object sender, EventArgs e) {
            var inputSender = (TextBox)sender;
            var value = GetInputAsInt(inputSender.Text);
            if (value == null)
                inputSender.Text = "3";
            else
                _simulationConfiguration.Countdown = (int)value;
        }
    }

    

    
}
